package com.example.demo1.user.view.controller;

import com.example.demo1.user.entity.User;
import com.example.demo1.user.service.IUserService;
import com.example.demo1.user.service.impl.UserServiceImpl;
import com.example.demo1.user.view.application.HomeApplication;
import com.example.demo1.user.view.application.RegisterApplication;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;

public class LoginController {
    @FXML
    private TextField txtLoginName;

    @FXML
    private PasswordField txtPassword;

    @FXML
    protected void doLogin() {
        String loginName = txtLoginName.getText();
        String password = txtPassword.getText();
        User user1 = new User();

        user1.setName(loginName);
        user1.setPassword(password);

        IUserService IUserService = new UserServiceImpl();
        Boolean loginCheck = IUserService.loginCheck(loginName, password);

//        Boolean flag = user1.getName().equals(user2.getName()) && user1.getPassword().equals(user2.getPassword());

        if (loginCheck) {
            //关闭当前界面，加载新的界面
            Platform.runLater(() -> {
                //创建主界面窗口
                try {
                    new HomeApplication().start(new Stage());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //关闭登陆窗口
                Stage stage = (Stage) txtLoginName.getScene().getWindow();
                stage.close();
            });
        } else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("提示信息！");
            alert.setContentText("用户名密码不正确");
            alert.show();
        }
    }

    @FXML
    protected void doCancel() {
        txtLoginName.setText("");
        txtPassword.setText("");
    }

    @FXML
    protected void doRegister() {
        Platform.runLater(() -> {
            //创建主界面窗口
            try {
                new RegisterApplication().start(new Stage());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //关闭登陆窗口
            Stage stage = (Stage) txtLoginName.getScene().getWindow();
            stage.close();
        });
    }
}